package cn.xorange.commons.data.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.xorange.commons.data.core.entity.BaseEntity;
import cn.xorange.commons.data.core.mapper.MBaseMapper;
import cn.xorange.commons.utils.Collect.CollectUtils;
import cn.xorange.commons.utils.lang.ObjectUtils;
import cn.xorange.commons.utils.lang.StringUtils;

import java.util.*;

/**
 * @author : yangjian
 * @date : 2022/2/23
 * @description :
 */
public class DataUtil {
    private DataUtil(){}

    public static String getJdbcType(String typeName){
        String type = "jdbcType";
        switch (typeName){
            case "java.lang.String":
                type = StringUtils.format("{}=VARCHAR",type);
                break;
            case "java.lang.Integer":
            case "int":
                type = StringUtils.format("{}=INTEGER",type);
                break;
            case "java.lang.Double":
            case "float":
                type = StringUtils.format("{}=DOUBLE",type);
                break;
            case "long":
                type = StringUtils.format("{}=BIGINT",type);
                break;
            case "java.lang.Boolean":
            case "boolean":
                type = StringUtils.format("{}=BOOLEAN",type);
                break;
            case "java.util.Date":
                type = StringUtils.format("{}=DATE",type);
                break;
            case "java.lang.Byte":
                type = StringUtils.format("{}=TINYINT",type);
                break;
            case "short":
                type = StringUtils.format("{}=SMALLINT",type);
                break;
            default:
                type = StringUtils.format("{}=VARCHAR",type);
                break;

        }
        return type;
    }

    public static Object formatObj(String typeName,Object object){
        switch (typeName){
            case "java.lang.String":
                return ObjectUtil.toString(object);
            case "java.lang.Integer":
            case "int":
                return ObjectUtils.toInt(object);
            case "java.lang.Float":
                return ObjectUtils.toFloat(object);
            case "java.lang.Double":
                return ObjectUtils.toDouble(object);
            case "long":
                return ObjectUtils.toLong(object);
            case "java.lang.Boolean":
            case "boolean":
                return ObjectUtils.toBoolean(object);
            case "java.util.Date":
                return ObjectUtils.toDate(object);
            case "java.lang.Byte":
                return ObjectUtils.toByte(object);
            case "short":
                return ObjectUtils.toShort(object);
            default:
                return object;
        }
    }


    /**
     * @param oldList 查询出来的原本携带的Id list
     * @param relationList 最终选择的Id list
     */
    public static <R extends BaseEntity,rM extends MBaseMapper<R>> Map<String, Collection<Object>> relationMap(Collection<Object> oldList, Collection<Object> relationList){
        Map<String,Collection<Object>> map = new HashMap<>(2);
        if(Objects.isNull(relationList)||relationList.isEmpty()){
            map.put("add", Collections.emptyList());
            map.put("remove", oldList);
        }else {
            Collection<Object> removeList = CollectUtils.subtract(oldList,relationList);
            Collection<Object> addList = CollectUtils.subtract(relationList,oldList);
            map.put("add",addList);
            map.put("remove",removeList);
        }
        return map;
    }



    //public static <T extends BaseEntity> void setTreeNode(T t, String dsType,boolean isMapUnderscoreToCamelCase){
    //
    //    Object id = ParamUtil.getKeyValue(t);
    //    Object pid = ReflectUtil.getFieldValue(t, FieldConstants.PARENT_ID);
    //    if (Objects.isNull(pid) || pid.equals(0)) {
    //        ReflectUtil.setFieldValue(t, FieldConstants.PARENT_ID, 0);
    //        ReflectUtil.setFieldValue(t, FieldConstants.TREE_NODE, id);
    //        ReflectUtil.setFieldValue(t, FieldConstants.TREE_GRADE, 0);
    //    } else {
    //        Object nodeID = ReflectUtil.getFieldValue(t, FieldConstants.TREE_NODE);
    //        ReflectUtil.setFieldValue(t, FieldConstants.PARENT_ID, pid);
    //        DaoHelperService daoHelperService = SpringUtils.getBean(DaoHelperService.class);
    //
    //        if (Objects.nonNull(nodeID)) {
    //            String[] nodes = nodeID.toString().split(FieldConstants.TREE_SEPARATOR);
    //            /// 如果treeNode 的最后一个id 不等于 新增的id 说明 非导入数据
    //            if (Objects.nonNull(id) && !id.toString().equals(nodes[nodes.length - 1])) {
    //                if (pid.toString().equals(nodes[nodes.length - 1])) {
    //                    // 正常情况下的 最后一级等于传过来的pid
    //                    ReflectUtil.setFieldValue(t, FieldConstants.TREE_NODE, nodeID + FieldConstants.TREE_SEPARATOR + id);
    //                    ReflectUtil.setFieldValue(t, FieldConstants.TREE_GRADE, nodes.length);
    //                } else {
    //                    /// 纠正非导入tree id等于最后一级节点 和 不等于pid的正常数据
    //                    TreeEntity treeEntity = daoHelperService.selectTreeEntity(t, pid, dsType,isMapUnderscoreToCamelCase);
    //                    ReflectUtil.setFieldValue(t, FieldConstants.TREE_NODE, treeEntity.getTreeGrade() + FieldConstants.TREE_SEPARATOR + id);
    //                    ReflectUtil.setFieldValue(t, FieldConstants.TREE_GRADE, treeEntity.getTreeGrade() + 1);
    //                }
    //            }
    //        } else {
    //            /// 纠正非导入tree id等于最后一级节点 和 不等于pid的正常数据
    //            TreeEntity treeEntity = daoHelperService.selectTreeEntity(t, pid, dsType,isMapUnderscoreToCamelCase);
    //            ReflectUtil.setFieldValue(t, FieldConstants.TREE_NODE, treeEntity.getTreeNode() + FieldConstants.TREE_SEPARATOR + id);
    //            ReflectUtil.setFieldValue(t, FieldConstants.TREE_GRADE, treeEntity.getTreeGrade() + 1);
    //        }
    //    }
    //}

    public static <T extends BaseEntity,rM extends MBaseMapper<T>> int bathAddRelationEntity(List<T> addList, rM mapper){
        int insertLength = addList.size();
        int i = 0;
        while (insertLength>500){
            List<T> lst = addList.subList(i,i+500);
            mapper.insertBath(lst);
            i = i + 500;
            insertLength = insertLength - 500;
        }
        if(insertLength>0){
            List<T> lst = addList.subList(i,i+insertLength);
            mapper.insertBath(lst);
        }
        return insertLength;
    }

}
